package models

import (
	orm "go-papers/database"
	"go-papers/tools"
	_ "time"
)

type MemberLog struct {
	LogId        int    `json:"logId" gorm:"type:int(11);primary_key"`  //
	LogMemberId  int    `json:"logMemberId" gorm:"type:int(11);"`       //
	LogName      string `json:"logName" gorm:"type:varchar(255);"`      //
	LogMemerName string `json:"logMemerName" gorm:"type:varchar(255);"` //
	LogTxt       string `json:"logTxt" gorm:"type:text;"`               //
	LogInfo      string `json:"logInfo" gorm:"type:varchar(255);"`      //
	CreatedAt    string `json:"createdAt" gorm:"type:varchar(255);"`    //
	UpdatedAt    string `json:"updatedAt" gorm:"type:varchar(255);"`    //
	DeletedAt    string `json:"deletedAt" gorm:"-"`                     //
	DataScope    string `json:"dataScope" gorm:"-"`
	Params       string `json:"params"  gorm:"-"`
}

func (MemberLog) TableName() string {
	return "member_log"
}

// 创建MemberLog
func (e *MemberLog) Create() (MemberLog, error) {
	var doc MemberLog
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取MemberLog
func (e *MemberLog) Get() (MemberLog, error) {
	var doc MemberLog
	table := orm.Eloquent.Table(e.TableName())
	if e.LogMemberId != 0 {
		table = table.Where("log_member_id = ?", e.LogMemberId)
	}
	if e.LogName != "" {
		table = table.Where("log_name = ?", e.LogName)
	}
	if e.LogMemerName != "" {
		table = table.Where("log_memer_name = ?", e.LogMemerName)
	}
	if e.LogTxt != "" {
		table = table.Where("log_txt = ?", e.LogTxt)
	}
	if e.LogInfo != "" {
		table = table.Where("log_info = ?", e.LogInfo)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取MemberLog带分页
func (e *MemberLog) GetPage(pageSize int, pageIndex int) ([]MemberLog, int, error) {
	var doc []MemberLog

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.LogMemberId != 0 {
		table = table.Where("log_member_id = ?", e.LogMemberId)
	}
	if e.LogName != "" {
		table = table.Where("log_name = ?", e.LogName)
	}
	if e.LogMemerName != "" {
		table = table.Where("log_memer_name = ?", e.LogMemerName)
	}
	if e.LogTxt != "" {
		table = table.Where("log_txt = ?", e.LogTxt)
	}
	if e.LogInfo != "" {
		table = table.Where("log_info = ?", e.LogInfo)
	}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新MemberLog
func (e *MemberLog) Update(id int) (update MemberLog, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("log_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除MemberLog
func (e *MemberLog) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("log_id = ?", id).Delete(&MemberLog{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *MemberLog) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("log_id in (?)", id).Delete(&MemberLog{}).Error; err != nil {
		return
	}
	Result = true
	return
}
